package ycl.system.service.impl;

import cn.hutool.core.util.ObjUtil;
import org.springframework.stereotype.Service;
import ycl.common.base.service.impl.BaseServiceImpl;
import ycl.common.utils.JSONObjectConvertUtil;
import ycl.system.entity.SysMenu;
import ycl.system.mapper.SysMenuMapper;
import ycl.system.req.SysMenuReq;
import ycl.system.res.SysMenuRes;
import ycl.system.service.SysMenuService;

import java.util.List;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author ycl
 * @since 2022-03-03 11:43:56
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl
		<SysMenuMapper, SysMenu, SysMenuReq, SysMenuRes>
		implements SysMenuService {

	@Override
	public SysMenuRes selectTree() {
		SysMenu sysMenu = this.baseMapper.selectRoot();
		SysMenuRes res = JSONObjectConvertUtil.convert(sysMenu, SysMenuRes.class);
		selectTreeByParent(res);
		return res;
	}

	private void selectTreeByParent(SysMenuRes parent) {
		if (ObjUtil.isNull(parent))
			return;
		Long parentId = parent.getId();
		List<SysMenuRes> contents = this.baseMapper.selectByParentId(parentId);
		contents.forEach(this::selectTreeByParent);
		parent.setChildren(contents);
	}
}
